package com.cn.delan.web.mvc.admin.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.cn.delan.web.mvc.admin.AdminConst;
import com.cn.delan.web.mvc.admin.service.IUnZipService;
import com.cn.delan.web.mvc.admin.service.IZipService;
import com.cn.delan.web.mvc.admin.service.impl.UnZipService;
import com.cn.delan.web.mvc.admin.service.impl.ZipService;
import com.cn.delan.web.mvc.core.action.AbstractAction;
/**
 * 压缩打包
 * @author BingGo
 *
 */
public class UnZipAction extends AbstractAction {

	@Override
	public ModelAndView handle(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String path = this.getParameter("path");
		//ajax方式获取压缩时的进度值时为ajax
		String type = this.getParameter("type");
		IUnZipService unzipService = (IUnZipService)this.getServletContext().getAttribute(AdminConst.CONTEXT_ATTR_UNZIPSERVICE);
		if(unzipService==null){
			unzipService = new UnZipService();
			this.getServletContext().setAttribute(AdminConst.CONTEXT_ATTR_UNZIPSERVICE, unzipService);
		}
		//压缩格式为原文件名_yyyyMMdd.zip
		unzipService = unzipService.unzip(path);
		//ajax获取进度值时用到的path
		request.setAttribute(AdminConst.REQUEST_ATTR_PATH, path.replaceAll("\\\\", "\\\\\\\\"));
		//错误消息
		request.setAttribute(AdminConst.REQUEST_ATTR_MESSAGE, this.getFormatMessage(unzipService.getMessage()));
		//进度值
		request.setAttribute(AdminConst.REQUEST_ATTR_PERCENT, unzipService.getPercent());
		//AJAX返回方式，用于刷新打包进度框
		if(type!=null && type.equals("ajax")){
			PrintWriter writer = response.getWriter();
			writer.print("{\"percent\":"+unzipService.getPercent()+"}");
			return null;
		}
		return this.getView(INPUT);
	}

}
